package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.BusinessDaoImpl;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        //        1.查询当前用户购物车中当前商家的所有的食品（目的是放入订单明细中）
//        2.创建订单，并获取到订单的编号
//        3.向订单明细表中批量添加明细的数据
//        4.清空购物车（条件：当前用户、当前商家）
//        提交事务
//        回滚事务
        int orderId=0;
        CartDao cartDao=new CartDaoImpl();
        OrdersDao dao=new OrdersDaoImpl();
        OrderdetailetDao orderDetailetDao=new OrderdetailetDaoImpl();
        try{
            //        开始事务
            DBUtil.beginTransaction();
            //        1.查询当前用户购物车中当前商家的所有的食品（目的是放入订单明细中）
            Cart cart=new Cart();
            cart.setUserId(userId);
            cart.setBusinessId(businessId);
            List<Cart> list=cartDao.listCart(cart);
//        2.创建订单，并获取到订单的编号
            Orders orders=new Orders();
            orders.setUserId(userId);
            orders.setBusinessId(businessId);
            orders.setDaId(daId);
            orders.setOrderTotal(orderTotal);
            orderId=dao.saveOrders(orders);
//        3.向订单明细表中批量添加明细的数据
            List<Orderdetailet>orderDetailetList=new ArrayList<>();
            for (Cart c:list){
                Orderdetailet od=new Orderdetailet();
                od.setOrderId(orderId);
                od.setFoodId(c.getFoodId());
                od.setQuantity(c.getQuantity());
                orderDetailetList.add(od);
            }
            orderDetailetDao.saveOrderDetailetBatch(orderDetailetList);
//        4.清空购物车（条件：当前用户、当前商家）
            cartDao.removeCart(cart);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            orderId = 0;
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orderId;
    }
    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDao dao = new OrdersDaoImpl();
        OrderdetailetDao orderDetailetDao=new OrderdetailetDaoImpl();
        try {
            DBUtil.getConnection();
//            根据订单id查询到订单的信息（多对一：商家）
            orders = dao.getOrdersById(orderId);
//            根据订单的id查询订单明细的信息
            List<Orderdetailet>list= orderDetailetDao.listOrderDetailetByOrderId(orderId);
            orders.setList(list);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }
    @Override
    public List<Orders> listOrdersByUserId(String userId){
        List<Orders> list = new ArrayList<>();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderDetailetDao = new OrderdetailetDaoImpl();
        try {
            DBUtil.getConnection();
            DBUtil.beginTransaction();
            //先查询当前用户所有订单
            list = ordersDao.listOrdersByUserId(userId);
            for(Orders o : list) {
                //在查询每个订单的明细
                List<Orderdetailet> odList = orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
                o.setList(odList);
            }
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return list;
    }
}
